package fr.ensisa.hassenforder.game.client;

import java.io.InputStream;
import java.util.ArrayList;

import fr.ensisa.hassenforder.game.model.Category;
import fr.ensisa.hassenforder.game.model.Product;
import fr.ensisa.hassenforder.network.BasicAbstractReader;
import fr.ensisa.hassenforder.network.FileHelper;
import fr.ensisa.hassenforder.network.Protocol;

public class Reader extends BasicAbstractReader {
    ArrayList al;
    boolean booldisconnect;
	public Reader(InputStream inputStream) {
		super(inputStream);
	}

	public ArrayList getParams() {
		return al;
	}

	public void receive() { 
		type = readInt();
		switch (type) {
		case Protocol.REP_CONNECT_OK:
			al = new ArrayList();
			al.add(readString());
			al.add(readLong());
			break;
		case Protocol.REP_CONNECT_KO:
			al = null;
			break;
		
		case Protocol.REP_DISCONNECT:
			al = new  ArrayList();
			al.add(readBoolean());
			//booldisconnect=readBoolean();
			break;
		case Protocol.REP_STRATISTIQUE:
			al= new ArrayList();
			al.add(readInt());
			al.add(readString());
			break;
		case Protocol.REP_ADD_CASH:
			al = new  ArrayList();
			al.add(readBoolean());
			break;
		case Protocol.REP_PICTURE:
			al = new  ArrayList();
			long l=readLong();
			String image = readString();
			FileHelper.writeContent("./res/"+image,readBytes(l));
			al.add("./res/"+image);
			break;
		case Protocol.REP_PRODUCTS:
			al = new  ArrayList();
			int i = readInt();
			ArrayList<Product> a = new ArrayList<Product>();
			Product p = null;
			for(int j=0;j<i;j++)
			{
				p = new Product(Category.valueOf(readString()),readString(),readString(),readInt(),readBoolean(),readInt(),readLong());
				a.add(p);
			}
			al.add(a);
			break;
		case Protocol.REP_SHOP:
			al = new  ArrayList();
			int i2 = readInt();
			ArrayList<Product> a2 = new ArrayList<Product>();
			Product p2 = null;
			for(int j=0;j<i2;j++)
			{
				p2 = new Product(Category.valueOf(readString()),readString(),readString(),readInt(),readBoolean(),readInt(),readLong());
				a2.add(p2);
			}
			al.add(a2);
			break;
		case Protocol.REP_REFRESH:
			al = new  ArrayList();
			al.add(readBoolean());
			break;
		case Protocol.REP_BUY:
			al = new  ArrayList();
			al.add(readBoolean());
			getParams().get(0);
			break;
		case Protocol.REP_SELL:
			al = new  ArrayList();
			al.add(readBoolean());
			break;
		case Protocol.REP_CLEAR:
			al = new  ArrayList();
			al.add(readBoolean());
			break;
		case Protocol.REP_CONSUME:
			al = new ArrayList();
			al.add(readBoolean());
			break;
		}
		
		
		
	}

public ArrayList getdisconnect() {
		
		return al;
	}

	public boolean getbooldisconnect()
	{
		return booldisconnect;
	}
}
